/**
 * @Date: Mar 8, 2010 8:07:06 PM
 */
package com.philip.journal.home.controller.action;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;

import com.philip.journal.core.Constant;
import com.philip.journal.core.controller.ResponseHandler;
import com.philip.journal.core.exception.JournalException;
import com.philip.journal.core.service.ServiceProxy;
import com.philip.journal.home.bean.Entry;
import com.philip.journal.home.service.util.BeanToMapConverter;

/**
 * @author cry30
 */
public class GetEntryDetailAction extends AbstractTreeAction {

    /** Data form field name. */
    private static final String DATA = "data";

    /** Entry Id form field name. */
    private transient String entryId;

    /**
     * Default delegate constructor.
     * 
     * @param serviceProxy Service proxy to be injected by Spring.
     * @param responseHandler Response handler to be injected by Spring.
     */
    public GetEntryDetailAction(final ServiceProxy serviceProxy, final ResponseHandler responseHandler) {
        super(serviceProxy, responseHandler);
    }

    @Override
    public void execute() throws ServletException, IOException {

        final long locEntryId = parseId(this.entryId);
        try {
            final Entry entry = getServiceProxy().getEntryDetail(locEntryId);
            final BeanToMapConverter entryConverter = getResponseHandler().getConverterFactoryMap().get(
                    Constant.BeanConverter.ENTRY_TO_JSON);
            final Map<String, Object> converted = entryConverter.convert(entry);
            converted.put("branchId", "b-" + entry.getBranch().getBranchId());
            getResponseHandler().respondSuccess(getHttpServletResponse(), converted, DATA);
        } catch (final JournalException e) {
            getLogger().debug(e.getMessage(), e);
            getResponseHandler().respondFail(getHttpServletResponse(), e.getMessage());
        }
    }

    /**
     * @param pEntryId setter for {@link #entryId}.
     */
    public void setEntryId(final String pEntryId) {
        this.entryId = pEntryId;
    }
}
